package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import Models.POSDiscount;

public class POSDiscountPresenter extends AbstractPresenter<POSDiscount> {
	final String SINGLEITEM = "posDiscount";
	final String MULTIITEM = "pOSDiscounts";
	
	public ArrayList<POSDiscount> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public POSDiscount tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public POSDiscount createItemFromJSONObject(JSONObject jsonDiscount){
		POSDiscount discount = new POSDiscount();
		try{
			discount.setDescription(jsonDiscount.getString("discountDescription"));
			discount.setFlatRate(jsonDiscount.getDouble("flat"));
			discount.setId(jsonDiscount.getInt("id"));
			discount.setPctRate(jsonDiscount.getDouble("pct"));
			return discount;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(POSDiscount item){
		try{
			JSONObject json = new JSONObject();
			json.put("discountDescription", item.getDescription());
			json.put("id", item.getId());
			json.put("flat", item.getFlatRate());
			json.put("pct", item.getPctRate());
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public POSDiscount getEmptyEntity(){
		POSDiscount entity = new POSDiscount();
		return entity;
	}
}
